<?php

namespace app\admin\controller;

use app\common\controller\Send;
use app\admin\model\Oauth AS OauthModel;
use app\common\enums\StatusEnum;

class Oauth extends Base
{
    use Send;

    /**
     * 用户列表
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $search = input('');

        $nickname = isset($search['nickname']) ? $search['nickname'] : '';
        $status = isset($search['status']) ? $search['status'] : -1;
        $sex = isset($search['sex']) ? $search['sex'] : -1;
        $phone = isset($search['phone']) ? $search['phone'] : '';
        $class_num =isset($search['class_num']) ? $search['class_num'] : -1;


        $startTime = isset($search['start_time']) ? $search['start_time'] : '';
        $endTime = isset($search['end_time']) ? $search['end_time'] : '';
        $search = [
            'nickname' => $nickname,
            'status' => $status,
            'sex' => $sex,
            'class_num' => $class_num,
            'phone' => $phone,
            'start_time' => $startTime,
            'end_time' => $endTime
        ];

        $where = [];
        if ($search['nickname']) {
            $where[] = ['nickname', 'like', '%' . $search['nickname'] . '%'];
        }
        if ($search['class_num']) {
            $where[] = ['class_num', '>=', $class_num];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        if ($search['sex'] != null && $search['sex'] != -1) {
            $where[] = ['sex', '=', $search['sex']];
        }

        if ($search['phone']) {
            $where[] = ['phone', 'like', '%' . $search['phone'] . '%'];
        }

        if ($search['start_time']) {
            $where[] = ['create_time', '>=', strtotime($search['start_time'])];
        }

        if ($search['end_time']) {
            $where[] = ['create_time', '<=', strtotime($search['end_time'])];
        }

        $model = OauthModel::getList($where);
        return view('index', [
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'sex' => OauthModel::attributes('sex'),
        ]);
    }

    /**
     * 批量启用、禁用
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disables()
    {
        $status = input('status');
        $ids = input('ids');
        OauthModel::disables($ids, $status);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        OauthModel::disabledOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    public function detail($id)
    {
        $model = OauthModel::getDetail($id);
        return view('detail', [
            'model' => $model,
        ]);
    }

    public function search($q)
    {
        $out = ['results' => ['id' => '', 'text' => '']];
        if (!$q) {
            return $out;
        }

        $data = OauthModel::searchOauth($q);
        $out['results'] = array_values($data);
        return json($out);
    }
    public function updateSet()
    {
        $id = input('id');
        $note = input('note');
        OauthModel::updateSet($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }
    public function updateNote()
    {
        $id = input('id');
        $note = input('note');
        OauthModel::updateNote($id,$note);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }
}
